// src/store/modules/user.js
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useUserStore = defineStore('user', () => {
  const user = ref(JSON.parse(localStorage.getItem('admin_user')) || null)
  const token = ref(localStorage.getItem('admin_token') || '')
  
  const hasToken = computed(() => {
    return !!token.value && token.value.length > 0
  })
  
  function setUser(userData) {
    user.value = userData
    localStorage.setItem('admin_user', JSON.stringify(userData))
  }
  
  function setToken(newToken) {
    token.value = newToken
    localStorage.setItem('admin_token', newToken)
  }
  
  function clearUser() {
    user.value = null
    token.value = ''
    localStorage.removeItem('admin_user')
    localStorage.removeItem('admin_token')
  }
  
  return {
    user,
    token,
    hasToken,
    setUser,
    setToken,
    clearUser
  }
})